<?php


require_once("login/src/Model/UserInfo.php");
require_once("Exercises/Model/MuscleGroup.php");
require_once("Database/Model/DatabaseConnection.php");


class MuscleGroupDAL  
{

     /**
    *@return MuscleGroup array
    **/
	public function getMuscleGroupList()
	{ 
		$sql = "SELECT `MuscleGroupID`, Name FROM mousclegroups";

		return $this->MuscleGroupList($sql);
	}

    /**
    *@param string sql
    *@throws new exceptions
    *@return MuscleGroup array 
    **/
	private function MuscleGroupList($sql)
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());
        
        $mysqli->set_charset('utf8');

		$stmt = $mysqli->prepare($sql);
                if ($stmt == FALSE) 
                {
                    throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
                }

                
                $result = $stmt->execute();
                if ($result == FALSE) 
                {
                    throw new Exception("execute of [$sql] failed " . $stmt->error);
              
                }

                $result = $stmt->bind_result($muscleGroupID, $name);
                if ($result == FALSE) 
                {
                    throw new Exception("execute of [$sql] failed " . $stmt->error);
                	echo "An error has occured:: ExerciseDAL3";
                }

                $muscleGroupList = array();

	            while ($stmt->fetch()) 
                {
	                $muscleGroupList[] = new MuscleGroup($muscleGroupID, $name);
	            }

	            $mysqli->close();
                return $muscleGroupList;
	}

}